package com.focusai.efairy.ui.activity.announce;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.focusai.efairy.R;
import com.focusai.efairy.model.project.ProjectItem;
import com.focusai.efairy.model.request.GetProjectListRequest;
import com.focusai.efairy.model.response.ProjectListResponse;
import com.focusai.efairy.network.HttpManager;
import com.focusai.efairy.network.Response;
import com.focusai.efairy.network.exception.NetworkException;
import com.focusai.efairy.ui.adapter.announce.SelectProjectAdapter;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerAdapter;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerViewHolder;
import com.focusai.efairy.ui.adapter.base.RecycleViewDivider;
import com.focusai.efairy.ui.base.SwipeBackBaseActivity;
import com.focusai.efairy.ui.widget.SearchTittleView;
import com.focusai.efairy.ui.widget.TitleView;
import com.focusai.efairy.utils.ScreenUtil;

import java.util.ArrayList;

/**
 * Created by luo_guangwen on 2017/10/24.
 */

public class SelectProjectActivity extends SwipeBackBaseActivity {
    public static final String KEY_PROJECT_ITEMS = "key_project_items";
    private TitleView mTitleView;
    private SearchTittleView searchTittleView;
    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout layoutRefresh;

    private SelectProjectAdapter mAdapter;
    private ArrayList<ProjectItem> mList;
    private ArrayList<ProjectItem> hasSelectedList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_select_project);
        if (getIntent() != null) {
            hasSelectedList = (ArrayList<ProjectItem>) getIntent().getSerializableExtra(KEY_PROJECT_ITEMS);
        }
        if (hasSelectedList == null) {
            hasSelectedList = new ArrayList<>();
        }
        initView();
        bindEvents();
    }

    @Override
    protected boolean hasActionBar() {
        return false;
    }

    @Override
    protected void initView() {
        mRecyclerView = findView(R.id.rcyc_view);
        mTitleView = findView(R.id.title_view);
        layoutRefresh = findView(R.id.layout_refresh);
        searchTittleView = findView(R.id.view_tittle_search);
    }

    @Override
    protected void bindEvents() {
        mList = new ArrayList<>();
        mAdapter = new SelectProjectAdapter(getContext());
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));

        mRecyclerView.addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.VERTICAL, ScreenUtil.dp2pix(1), getResources().getColor(R.color.main_line_gray)));
        mAdapter.refreshData(mList);
        mAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseRecyclerViewHolder holder) {
                int position = mRecyclerView.getChildLayoutPosition(holder.itemView);
                ProjectItem item = mList.get(position);
                item.isSelected = !item.isSelected;
                mAdapter.notifyDataSetChanged();
            }
        });
        layoutRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadData("");
            }
        });
        searchTittleView.setSearchListener(new SearchTittleView.OnSearchListener() {
            @Override
            public void onStartQuery(String keywork, boolean hideInput) {
                if (hideInput) {
                    hideSoftInput();
                }
                loadData(keywork);
            }

            @Override
            public void onCancalBackClick() {
                mTitleView.setVisibility(View.VISIBLE);
                hideSoftInput();
                loadData("");
            }

            @Override
            public void onEnterInputKeyWork() {
                mTitleView.setVisibility(View.GONE);
            }
        });
        mTitleView.getmTextRight().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ArrayList<ProjectItem> items = new ArrayList<>();
                for (ProjectItem item : mList) {
                    if (item.isSelected) {
                        items.add(item);
                    }
                }
                if (items.size() == 0) {
                    showToast("请选择项目");
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra(KEY_PROJECT_ITEMS, items);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        mTitleView.getmBack().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        layoutRefresh.setRefreshing(true);
        loadData("");
    }

    private void loadData(String keyword) {
        HttpManager.getInstance().sendRequest(new GetProjectListRequest(keyword, new Response.Listener<ProjectListResponse>() {
            @Override
            protected void onSuccess(ProjectListResponse response) {
                layoutRefresh.setRefreshing(false);
                if (response != null && response.project_list != null) {
                    mList.clear();
                    mList.addAll(response.project_list);
                    doSlectedItems();
                    mAdapter.refreshData(mList);
                }
            }

            @Override
            protected void onFail(NetworkException exception) {
                layoutRefresh.setRefreshing(false);
                doException(exception);
            }
        }));
    }

    private void doSlectedItems() {
        for (ProjectItem selectItem : hasSelectedList) {
            for (ProjectItem item : mList) {
                if (selectItem.efairyproject_id == item.efairyproject_id) {
                    item.isSelected = true;
                    break;
                }
            }
        }
    }

}
